<template>
    <div class="common courseAdd">
        <innerCommon 
            :location="location"
        >
            <div slot="right" class="top">
                <a-button class="returnBack" type="primary" @click="returnBack()">返回</a-button>
            </div>
            <div slot="table" class="tableWrap">
                <eform
                    :formData="formData"
                    ref="form"
                    :fields="fields"
                    v-if="fields||type==0"
                >
                    <div slot="examTime">
                        <rangeTime 
                            @getTimes="getExamTimes"
                            :defaultTime="defaultExamTime"
                            v-decorator="['examTime',{rules: [{ required: true, message: '请选择考试时间' }]}]"
                        ></rangeTime>
                    </div>
                    <div slot="joinTime">
                        <rangeTime
                            @getTimes="getJoinTime"
                            :defaultTime="defaultJoinTime"
                            v-decorator="['joinTime',{rules: [{ required: true, message: '请选择报名时间' }]}]"
                        ></rangeTime>
                    </div>
                    <div slot="examType">
                        <examType
                            :defaultExamType="defaultExamType"
                            @changeExamType="changeExamType"
                            v-decorator="['examType',{rules: [{ required: true, message: '请选择考试类型' }]}]"
                        ></examType>
                    </div>
                    <div slot="examIntroduction">
                        <eQuill
                            class="examineeQuill"
                            @getQuillContent="getQuillContent"
                            v-decorator="['info',{rules: [{ required: true, message: '请输入内容' }]}]"
                            :defaultData="quillInfo"
                        ></eQuill>
                    </div>
                </eform>
                <div class="submitBtn">
                    <a-button @click="returnBack()">取消</a-button>
                    <a-button type="primary" @click="save()">保存</a-button>
                </div>
            </div>
        </innerCommon>
    </div>
</template>
<script>
import {addExamineeApi,updateExamineeApi,getExamineeDetailApi} from "@/request/api"
import rangeTime from '@/components/common/rangeTime'
import examType from '@/components/common/examType'
import eQuill from '@/components/common/eQuill'
import eform from "@/components/common/form"
import innerCommon from "@/components/common/innerCommon"
import moment from "moment"
export default {
    name:'examinationAdd',
    components:{eform,rangeTime,examType,eQuill,innerCommon},
    created(){
        this.examId=this.$route.query.data
        if(this.examId){
            this.type=1
            this.editInit()
        }else{
            this.type=0
        }
        this.location=[
            {name:'1+X管理',path:''},{name:'考务管理',path:'/examineeManage'},{name:this.type==0?'新增':'修改',path:''}
        ]
    },
    data(){
        let validateExamineeName=(rule,value,callback)=>{
            if(value){
                if(value.trim().length === 0){
                    callback(new Error('考试名称不能全是空格'))
                }else{
                    callback()
                }
            }else{
                callback()
            }
        }
        const formData=[
            {
                label:"考试名称",
                prop:"title",
                placeholder:'请输入考试名称',
                rules:[{required:true,message:'请输入考试名称'},{validator:validateExamineeName}],
            },{
                label:"考试时间",
                scopedSlots: { customRender: 'examTime'},
            },{
                label:"报名时间",
                scopedSlots: { customRender: 'joinTime'},
            },{
                label:"考试类别",
                scopedSlots: { customRender: 'examType'},
            },{
                label:"考试设备",
                prop:"device",
                placeholder:'请输入考试设备',
                rules:[{required:true,message:'请输入考试设备'}],
            },{
                label:"设备型号",
                prop:"deviceType",
                placeholder:'请输入设备型号',
                rules:[{required:true,message:'请输入设备型号'}],
            },{
                label:"考生容量",
                prop:"examineeAmount",
                placeholder:'请输入考生容量',
                rules:[{required:true,message:'请输入考生容量'}],
            },{
                label:"考试介绍",
                scopedSlots: { customRender: 'examIntroduction'},
            }
        ]
        return{
            fields:null,
            formData,
            location:[],
            quillInfo:'',
            format:'YYYY-MM-DD HH:mm:ss',
            defaultExamTime:null,//考试时间
            defaultJoinTime:null,
            defaultExamType:null,
            type:0,//0是新增 1是修改
            examId:''
        }
    },
    methods:{
        editInit(){
            this.getToken(getExamineeDetailApi,{examId:this.examId},res=>{
                let info=res.data
                this.fields={
                    ...res.data,
                    'examTime':{'start':info.activityStart,'end':info.activityEnd},
                    'joinTime':{'start':info.startTime,'end':info.endTime}
                }
                this.defaultExamTime={'start':moment(res.data.activityStart),'end':moment(res.data.activityEnd)}
                this.defaultJoinTime={'start':moment(res.data.startTime&&res.data.startTime),'end':moment(res.data.endTime)}
                this.defaultExamType=[res.data.profession.id,res.data.skill.id,res.data.examType]
                this.quillInfo=res.data.info
            })
        },
        moment,
        returnBack(){
            this.$router.go(-1)
        },
        getExamTimes(start,end){
            this.$refs.form.form.setFieldsValue({['examTime']:{start,end}})
        },
        getJoinTime(start,end){
            this.$refs.form.form.setFieldsValue({['joinTime']:{start,end}})
        },
        changeExamType(data){
            if(data.length==3){
                this.$refs.form.form.setFieldsValue({['examType']:data})
            }
        },
        getQuillContent(data){
            this.$refs.form.form.setFieldsValue({['info']:data})
        },
        // 添加考务
        save(){
            this.$refs.form.form.validateFields().then(res=>{
                let formData={
                    ...res,
                    'startTime':moment(res.joinTime.start).format(this.format),
                    'endTime':moment(res.joinTime.end).format(this.format),
                    'activityStart':moment(res.examTime.start).format(this.format),
                    'activityEnd':moment(res.examTime.end).format(this.format),
                    'examType':res.examType[2],
                    'professionId':res.examType[0],
                    'skillId':res.examType[1],
                }
                let api
                if(this.type==0){
                    api=addExamineeApi
                }else{
                    api=updateExamineeApi
                    formData={...formData,'examId':this.fields.id}
                }
                this.getToken(api,formData,res=>{
                    this.$message.success(res.message)
                    this.returnBack()
                })
            })
        },
    }
}
</script>